#include "math/xobject.h"

#include "math/xtable.h"

NS_MATH_BEGIN

// XObjectData
xstring XObjectData::name()const
{
	return m_sName;
}

XObjectData* XObjectData::Clone()const
{
	return NULL;
}

// XObject
xstring XObject::name() const
{
	return ptr->name();
}

XObject XObject::Clone()const
{
	XObject ret;
	ret.ptr = ptr->Clone();
	return ret;
}

XObject::XObject()
{
	set_data(NULL);
}
XObject::XObject(XObject& obj)
{
	ptr = obj.ptr;
}
XObject::XObject(XObjectData* data_ptr)
{
	set_data(data_ptr);
}

bool XObject::isnil()const{return ptr==NULL;}

void XObject::set_data(XObjectData* ptr_)
{
	ptr = ptr_;
}

std::map<xstring, XObject> XObject::obj_map;
void XObject::init()
{
	static bool first = true;
	if(first)
	{
		// Register Objects
		obj_map["nil"] = XObject();
		obj_map["table"] = XObject(new xtable);
		first = false;
	}
}

XObject XObject::Create(const xstring& obj_typename)
{
	init();
	if(obj_map.find(obj_typename)!=obj_map.end())
	{
		return obj_map[obj_typename].Clone();
	}else
	{
		return obj_map["nil"];
	}
}

NS_MATH_END